package com.ios.util;

import java.io.*;
import java.nio.file.StandardCopyOption;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;

/**
*@Author:WenTao
*@Description:FM音频上传工具类
*@Date:15:24 2018/7/24
*/
public class FMUtil {
    private static final Random r = new Random();
    private static final SimpleDateFormat sDateFormate = new SimpleDateFormat("yyyyMMddHHmmss");
    /*获取音频存储，返回存储路径*/
    public static String ManageFM(InputStream FMInputStream, String fileName, String targetAddr){
        /*获取文件拓展名后缀*/
        String lastname = getFileExtension(fileName);
        /*获取保存到服务器后的文件名*/
        String realFileName = getRandomFileName();
        /*绝对路径*/
        String relativeAddr = targetAddr + realFileName + lastname;
        File file = new File(relativeAddr);
        /*FMinputStream写入文件*/
        try {
            java.nio.file.Files.copy(FMInputStream,file.toPath(), StandardCopyOption.REPLACE_EXISTING);
        }catch (Exception e){
            e.printStackTrace();
        }
        return relativeAddr;
    }

    /*获取输入文件流拓展名后缀*/
    private static String getFileExtension(String fileName){
        return fileName.substring(fileName.lastIndexOf("."));
    }

    /*生成新文件名**/
    private static String getRandomFileName(){
        /*当前修改时间加四位随机数*/
        int rannum = r.nextInt(8999) + 1000;
        String nowTimeStr = sDateFormate.format(new Date());
        return nowTimeStr + rannum;
    }
}
